home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / ZIPPED / DOS / GRAPHICS / POVSRC.ZIP / MACHINE.ZIP / AMIGA.ZIP / AMIGA.C next >
C/C++ Source or Header  |  1992-07-09  |  22KB  |  749 lines

  1. /****************************************************************************
  2. *                   amiga.c
  3. *
  4. *  This module handles all of the Amiga-specific code for the raytracer.
  5. *
  6. *  from Persistence of Vision Raytracer 
  7. *  Copyright 1992 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. *  Copying, distribution and legal info is in the file povlegal.doc which
  10. *  should be distributed with this file. If povlegal.doc is not available
  11. *  or for more info please contact:
  12. *
  13. *       Drew Wells [POV-Team Leader] 
  14. *       CIS: 73767,1244  Internet: 73767.1244@compuserve.com
  15. *       Phone: (213) 254-4041
  16. * This program is based on the popular DKB raytracer version 2.12.
  17. * DKBTrace was originally written by David K. Buck.
  18. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  19. *
  20. *****************************************************************************/
  21.  
  22.  
  23. #include "frame.h"
  24. #include "povproto.h"
  25.  
  26. #include <proto/exec.h>
  27. #include <proto/intuition.h>
  28. #include <proto/graphics.h>
  29. #include <proto/dos.h>
  30. #include <proto/expansion.h>
  31. #include <exec/types.h>
  32. #include <intuition/intuition.h>
  33. #include <graphics/display.h>
  34. #include <libraries/expansionbase.h>
  35.  
  36. void geta4(void);
  37. void Requestor_Handler(void);
  38. void Amiga_open(void);
  39. void Amiga_close(void);
  40. void open_requestor(void);
  41. void close_requestor(void);
  42. void write_byte(int x, int y, unsigned char n);
  43. void write_hame_pixel(int x, int y, char Red, char Green, char Blue);
  44.  
  45. void write_cookie(unsigned char *brand, int line);
  46. void make_hame_palette(struct ViewPort *vp);
  47. void SetRGB8 (short reg, unsigned char rr, unsigned char gg,
  48.               unsigned char bb, short base);
  49.  
  50. void OpenFirecracker (void);
  51. void    write_firecracker_pixel(UWORD x, UWORD y, UBYTE r, UBYTE g, UBYTE b);
  52.  
  53. extern unsigned int Options;
  54. extern char DisplayFormat;
  55. extern FRAME     Frame;
  56.  
  57. #define INT_REV 29L
  58. #define GR_REV 29L
  59.  
  60. struct IntuitionBase *IntuitionBase;
  61. struct GfxBase *GfxBase;
  62. struct ExpansionBase *ExpansionBase;
  63.  
  64. struct Screen *s;
  65. volatile struct Window *w;
  66. struct Task *Requestor_Task;
  67.  
  68. volatile int Requestor_Running;
  69. volatile extern int Stop_Flag;
  70.  
  71. #define SCREEN_WIDTH 320
  72. #define SCREEN_HEIGHT 400
  73.  
  74. #define HAME_SCREEN_WIDTH 640
  75. #define HAME_SCREEN_HEIGHT 402
  76.  
  77. struct NewScreen Ham_Screen =
  78.    {
  79.    0, 0,
  80.    SCREEN_WIDTH, SCREEN_HEIGHT,
  81.    6,
  82.    0, 1,
  83.    INTERLACE | HAM,
  84.    SCREENQUIET,
  85.    NULL,
  86.    (UBYTE *) "POV-Ray",
  87.    NULL,
  88.    NULL
  89.    };
  90.  
  91.  
  92. struct NewScreen Ham_E_Screen =
  93.    {
  94.    0, 0,
  95.    HAME_SCREEN_WIDTH, HAME_SCREEN_HEIGHT,
  96.    4,
  97.    0, 1,
  98.    INTERLACE | HIRES,
  99.    SCREENQUIET,
  100.    NULL,
  101.    (UBYTE *) "POV-Ray",
  102.    NULL,
  103.    NULL
  104.    };
  105.  
  106. int lacer; /* if non-zero, screen is an interlace screen. Set this... */
  107.            /* ...as soon as you open your HAM-E screen.               */
  108.  
  109. unsigned char *fp0,*fp1,*fp2,*fp3; /* These are pointers which have been... */
  110.                                    /* ...cached from the screens BitMap[]   */
  111.                                    /* ...array. This allows us to get at    */
  112.                                    /* ...them much faster. Set them as soon */
  113.                                    /* ...as you open your HAM-E screen.     */
  114.  
  115. unsigned char bitpat[] =    /* This table is used as a table of masks... */
  116.   {                         /* ...to isolate bits in the HAM-E pixels    */
  117.     128,64,32,16,8,4,2,1,
  118.   };
  119.  
  120. unsigned char ham_cookie[] =  /* ham mode cookie... preceeds any HAM...    */
  121.   {                           /* ...color registers, and triggers hardware */
  122.     0xA2,0xF5,0x84,0xDC,      /* ...into ham mode.                         */
  123.     0x6D,0xB0,0x7F,0x18
  124.   };
  125.  
  126. struct Window *Requestor_Window;
  127. volatile struct MsgPort *Requestor_Port;
  128.  
  129. struct IntuiText chip Body_Text =
  130.    {0, 1, JAM1, 5, 10, NULL, (UBYTE *) "Click to abort the picture", NULL};
  131.  
  132. struct IntuiText chip Abort_Text =
  133.    {0, 1, JAM1, 5, 3, NULL, (UBYTE *) "Abort", NULL};
  134.  
  135. UWORD chip ColorTbl[16] = { 0x000, 0x111, 0x222, 0x333, 0x444, 0x555, 0x666,
  136.                        0x777, 0x888, 0x999, 0xaaa, 0xbbb, 0xccc, 0xddd,
  137.                        0xeee, 0xfff };
  138.  
  139. LONG last_red = 0, last_green = 0, last_blue = 0, last_y = -1;
  140.  
  141. /* Firecracker routines */
  142. typedef struct BOARD {
  143.     ULONG    orgb;
  144.     ULONG    orgb2;        /* autoincrementing */
  145.     UWORD    pad;
  146.     UBYTE    control0;
  147.     UBYTE    control1;
  148.     UWORD    y, x;
  149. } BOARD;
  150.  
  151. BOARD        *board;
  152. int fc_width;
  153.  
  154. void amiga_init_POVRAY(void )
  155.    {
  156.    (void) onbreak(amiga_close_all);
  157.    }
  158.  
  159. int matherr (x)
  160.    struct exception *x;
  161.    {
  162.    fprintf (stderr, "Math error type: %d from function %s values: %g %g\n",
  163.            x->type, x->name, x->arg1, x->arg2);
  164.  
  165.    switch(x->type) 
  166.      {
  167.      case DOMAIN:
  168.      case OVERFLOW:
  169.         x->retval = 1.0e17;
  170.         break;
  171.  
  172.      case SING:
  173.      case UNDERFLOW:
  174.         x->retval = 0.0;
  175.         break;
  176.  
  177.      case TLOSS:
  178.      case PLOSS:
  179.         return (0);
  180.  
  181.      default:
  182.         break;
  183.      }
  184.    return(1);
  185.    }
  186.  
  187. void Requestor_Handler ()
  188.    {
  189.    Requestor_Port = CreatePort ("ray trace port", 0L);
  190.    Requestor_Window = BuildSysRequest
  191.              (NULL, &Body_Text, NULL, &Abort_Text, GADGETUP, 280L, 60L);
  192.    Wait ((1 << Requestor_Port -> mp_SigBit)
  193.           | (1 << Requestor_Window -> UserPort -> mp_SigBit));
  194.  
  195.    Requestor_Running = FALSE;
  196.    Stop_Flag = TRUE;
  197.    }
  198.  
  199. void Amiga_open()
  200.    {
  201.    IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library",INT_REV);
  202.    if (IntuitionBase == NULL)
  203.      exit(FALSE);
  204.  
  205.    GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", GR_REV);
  206.    if (GfxBase == NULL)
  207.      exit(FALSE);
  208.    Requestor_Running = FALSE;
  209.    }
  210.  
  211. void Amiga_close()
  212.    {
  213.    if (Requestor_Running) {
  214.       Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  215.       Delay (2L);
  216.       }
  217.  
  218.    if (Requestor_Window)
  219.       FreeSysRequest (Requestor_Window);
  220.  
  221.    Requestor_Window = NULL;
  222.  
  223.    CloseLibrary (GfxBase) ;
  224.    CloseLibrary (IntuitionBase) ;
  225.    }
  226.  
  227. void open_requestor()
  228.    {
  229.    Requestor_Window = NULL;
  230.    Stop_Flag = FALSE;
  231.    Requestor_Running = TRUE;
  232.    Requestor_Task = CreateTask ("Raytrace Requestor", 2L,
  233.                                 (APTR) Requestor_Handler, 20000L);
  234.    }
  235.  
  236. void display_finished ()
  237.    {
  238.    if (Requestor_Running) {
  239.      Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  240.      Delay (2L);
  241.      }
  242.  
  243.    if (Requestor_Window)
  244.       FreeSysRequest (Requestor_Window);
  245.  
  246.    Requestor_Window = NULL;
  247.    if (Options & PROMPTEXIT)
  248.       {
  249.       printf ("Finished.\nPress CR to quit.\n");
  250.       getchar();
  251.       }
  252.    }
  253.  
  254. void    OpenFirecracker() {
  255.     struct ConfigDev    *dev = NULL;
  256.     ULONG            i;
  257.  
  258.     ExpansionBase = (struct ExpansionBase *) 
  259.                            OpenLibrary("expansion.library", 0L);
  260.     if (!ExpansionBase) {
  261.         printf("No expansion library\n");
  262.         exit(999);
  263.     }
  264.  
  265.     dev = FindConfigDev(NULL, 0x838, 0);
  266.     if (dev) {
  267.         board = (BOARD *)dev->cd_BoardAddr;
  268.     }
  269.     else {
  270.         printf("Can't find FireCracker\n");
  271.         exit(999);
  272.     }
  273.     CloseLibrary((struct Library *) ExpansionBase);
  274.         if (Frame.Screen_Width > 768) {
  275.        board->control0 = 0xf8;
  276.            fc_width = 1024;
  277.            }
  278.         else if (Frame.Screen_Width > 512) {
  279.        board->control0 = 0xb8;           
  280.            fc_width = 768;
  281.            }
  282.         else if (Frame.Screen_Width > 384) {
  283.        board->control0 = 0x78;
  284.            fc_width = 512;
  285.            }
  286.         else {
  287.        board->control0 = 0x38;
  288.            fc_width = 384;
  289.            }
  290.  
  291.     board->control1 = 0x7f;
  292.     board->y = 0; board->x = 0;
  293.     for (i=0; i<fc_width*482; i++) {
  294.            /* I write to the auto-increment and non-autoincrement registers
  295.               to make this work on 68000's as well as 68020's. */
  296.            board->orgb  = 0x40404040;
  297.            board->orgb2 = 0x40404040;
  298.            }
  299. }
  300.  
  301.  
  302. void    write_firecracker_pixel(x,y, r,g,b)
  303. UWORD    x,y;
  304. UBYTE    r,g,b;
  305. {
  306.     ULONG        v1,v2,v3,v;
  307.         int             new_x, new_y;
  308.  
  309.     v1 = r; v2 = g; v3 = b;
  310.     v = ((v1<<16)&0xff0000) + ((v2<<8)&0xff00) + (v3&0xff);
  311.  
  312.     new_y = y+(482-Frame.Screen_Height)/2; new_x = x+(fc_width-Frame.Screen_Width)/2;
  313.         if ((new_x >= 0) && (new_y >= 0)
  314.             && (new_x < fc_width) && (new_y < 482)) {
  315.            board->y = new_y;
  316.            board->x = new_x;
  317.        board->orgb2 = v;
  318.            }
  319. }
  320.  
  321. /*:
  322. .n write_byte()
  323. .k low_level write_byte byte_write write_pixel
  324. .b
  325.  
  326. SYNOPSIS:   void write_byte(x,y,n);
  327.                 short x;
  328.                 int y;
  329.                 unsigned char n;
  330.  
  331. FUNCTION:   This function is similar to the Amiga's WritePixel routine.
  332.             is simply takes the incoming position and value and writes
  333.             them to the HAM-E screen appropriately.
  334.  
  335. INPUTS:     x - horizontal position on screen.
  336.             y - vertical position on screen.
  337.             n - value 0-255 to write in the pixel.
  338.  
  339. RESULTS:    None
  340.  
  341. BUGS:       None Known.
  342.  
  343. LIMITATIONS:Hard coded for screen width of 320 pixels (640 hi-res pixels)
  344.  
  345. SEE ALSO:   Global variables "fp3", "fp2", "fp1", "fp0", "bitpat[]"
  346.  
  347. :*/
  348.  
  349. void write_byte(x,y,n)
  350.   int x;
  351.   int y;
  352.   unsigned char n;
  353.   {
  354.   register int ypos,byte_offset;
  355.   register short bit_offset;
  356.     ypos = y * 80; /* index to correct scan line - note hard coded width! */
  357.     bit_offset = (x << 1) & 7; /* find base bit position */
  358.     byte_offset = (x >> 2);   /* find base byte offset */
  359.     if (n & 128) *(fp3 + ypos + byte_offset) |= bitpat[bit_offset];
  360.             else *(fp3 + ypos + byte_offset) &= ~bitpat[bit_offset];
  361.     if (n &  64) *(fp2 + ypos + byte_offset) |= bitpat[bit_offset];
  362.             else *(fp2 + ypos + byte_offset) &= ~bitpat[bit_offset];
  363.     if (n &  32) *(fp1 + ypos + byte_offset) |= bitpat[bit_offset];
  364.             else *(fp1 + ypos + byte_offset) &= ~bitpat[bit_offset];
  365.     if (n &  16) *(fp0 + ypos + byte_offset) |= bitpat[bit_offset];
  366.             else *(fp0 + ypos + byte_offset) &= ~bitpat[bit_offset];
  367.     bit_offset++; /* to next nybble */
  368.     if (bit_offset == 8) /* carry into next byte? */
  369.       {
  370.         bit_offset=0;
  371.         byte_offset++;
  372.       }
  373.     if (n & 8) *(fp3 + ypos + byte_offset) |= bitpat[bit_offset];
  374.           else *(fp3 + ypos + byte_offset) &= ~bitpat[bit_offset];
  375.     if (n & 4) *(fp2 + ypos + byte_offset) |= bitpat[bit_offset];
  376.           else *(fp2 + ypos + byte_offset) &= ~bitpat[bit_offset];
  377.     if (n & 2) *(fp1 + ypos + byte_offset) |= bitpat[bit_offset];
  378.           else *(fp1 + ypos + byte_offset) &= ~bitpat[bit_offset];
  379.     if (n & 1) *(fp0 + ypos + byte_offset) |= bitpat[bit_offset];
  380.           else *(fp0 + ypos + byte_offset) &= ~bitpat[bit_offset];
  381.   }
  382.  
  383. /*:
  384. .n write_cookie()
  385. .k cookie low_level setup configure
  386. .b
  387.  
  388. SYNOPSIS:   void write_cookie(brand,line);
  389.                 unsigned char *brand;
  390.                 int line;
  391.  
  392. FUNCTION:    This function writes the cookie on a particular line.
  393.              The variable "brand" is a pointer to an arrary of data
  394.              that contains the particular cookie for the mode you want.
  395.              These arrays are the global ones "ham_cookie[]" and
  396.              "reg_cookie[]".
  397.              
  398.              Call as:
  399.              
  400.                   write_cookie(ham_cookie,line);
  401.                               -or-
  402.                   write_cookie(reg_cookie,line);
  403.              
  404.              Note: If you have a four line palette, you need to call
  405.              this function for each successive line the palette exists
  406.              upon, for example:
  407.              
  408.                   write_cookie(reg_cookie,0);
  409.                   write_cookie(reg_cookie,1);
  410.                   write_cookie(reg_cookie,2);
  411.                   write_cookie(reg_cookie,3);
  412.                   
  413.                         -or-
  414.                   
  415.                   for (i=0; i<4; i++)
  416.                     {
  417.                       write_cookie(reg_cookie,i);
  418.                     }
  419.              
  420.              If the global variable "lacer" is set, this function will
  421.              write the cookie data on the appropriate lines in both
  422.              fields... sending four lines of cookie to the function
  423.              with the lines 0,1,2,3 will write cookies on line pairs
  424.              0-1, 2-3, 4-5, and 6-7.
  425.  
  426. INPUTS:     A pointer to the apropriate cookie and the line to put it on.
  427.  
  428. RESULTS:    None
  429.  
  430. BUGS:       None Known.
  431.  
  432. LIMITATIONS:None Known.
  433.  
  434. SEE ALSO:   The global variable "lacer" and the "ham_cookie[]"
  435.             and reg_cookie[] global arrays.
  436.  
  437. :*/
  438. void write_cookie(brand,line)
  439.   unsigned char *brand;
  440.   int line;
  441.   {
  442.   int i;
  443.     if (lacer) /* we need double the cookie data! */
  444.       {
  445.         for (i=0; i<8; i++)
  446.           {
  447.             write_byte(i,line*2,brand[i]);
  448.             write_byte(i,(line*2)+1,brand[i]);
  449.           }
  450.       }
  451.     else
  452.       {
  453.         for (i=0; i<8; i++)
  454.           {
  455.             write_byte(i,line,brand[i]);
  456.           }
  457.       }
  458.   }
  459.  
  460. /*:
  461. .n make_hame_palette()
  462. .k amiga_palette palette_amiga setup configure
  463. .b
  464.  
  465. SYNOPSIS:   void make_hame_palette(vp);
  466.                 struct ViewPort *vp;
  467.  
  468. FUNCTION:   Sets palette for Amiga side of hardware: The palette here is
  469.             designed to achieve two independant goals. First, and most
  470.             importantly, it creates a situation where the IRGB lines at
  471.             the Amiga's data port will exactly mirror the data in the
  472.             bitplanes of the screen as each pixel is emitted. 
  473.             Secondly, this palette makes the images visible, if not
  474.             sensible, on a non HAM-e equipped Amiga... Hopefully this
  475.             distinctive color palette will quickly cue the user that
  476.             they are missing something good. :^)
  477.             
  478.             IRGB to 12 bit correspondence:
  479.             
  480.             bit 8 - b3 of red
  481.             bit 4 - b3 of green
  482.             bit 2 - b3 of blue
  483.             bit 1 - b0 of blue
  484.  
  485. INPUTS:     A pointer to the ViewPort that "belongs" to this screen.
  486.  
  487. RESULTS:    None
  488.  
  489. BUGS:       None Known.
  490.  
  491. LIMITATIONS:None Known.
  492.  
  493. SEE ALSO:   
  494.  
  495. :*/
  496. void make_hame_palette(vp)
  497.   struct ViewPort *vp;
  498.   {
  499.   int rr,gg,bb,i;
  500.   int col;
  501.     col=0;
  502.     for (i=0; i<16; i++)
  503.       {
  504.         rr=0; gg=0; bb=0;
  505.         if (i & 8) rr  = 8; /* this builds the IRGB bit outputs...   */
  506.         if (i & 4) gg  = 8; /* ...these four bits are all that are   */
  507.         if (i & 2) bb  = 8; /* ...required to make the HAM-E run,    */
  508.         if (i & 1) bb |= 1; /* ...they xfer b0->b3 to the IRGB lines */
  509.         if (i != 0)     /* we don't mess with c0 - we leave it black */
  510.           {
  511.             rr += (col & 7); /* build strange colors in cregs 1-15...  */
  512.             col += 2;        /* ...these extra bits sent to the color  */
  513.             gg += (col & 7); /* ...registers make amiga palette very   */
  514.             col += 2;        /* ...interesting to look at if the HAM-E */
  515.             bb += (col & 6); /* ...is NOT attached. Otherwise useless. */
  516.             col += 2;        /* ...The code in this "if" is optional.  */
  517.           }
  518.         SetRGB4(vp,i,rr,gg,bb); /* this actually sets the Amiga color regs */
  519.       }
  520.   }
  521.  
  522. /*:
  523. .n SetRGB8()
  524. .k palette_hame hame_palette setup configure
  525. .b
  526.  
  527. SYNOPSIS:   void SetRGB8(reg,rr,gg,bb,base);
  528.                 int reg,rr,gg,bb,base;
  529.  
  530. FUNCTION:   This routine sets the color registers in the HAM-E hardware.
  531.             It can handle color registers located at any point on screen,
  532.             by setting the "base" variable to the starting scan line where
  533.             the color registers exist. Normally, the cookie and it's
  534.             associated color registers are located beginning at scan line 0.
  535.             If the screen is an interlace screen, this routine will set both
  536.             set of color registers identically; when in interlace, the
  537.             HAM-E maintains separate sets of color registers for the
  538.             odd and even interlace fields. Since this routine sets both
  539.             sets of color registers identically, you don't have to
  540.             worry about dealing with this feature.
  541.             
  542. INPUTS:     reg  - the color register number from 0 to 255 to be set
  543.             rr   - the red value from 0-255
  544.             gg   - the green value from 0-255
  545.             bb   - the blue value from 0-255
  546.             base - the starting scan line of the first coookie position
  547.                    (usually zero)
  548.  
  549. RESULTS:    None
  550.  
  551. BUGS:       None Known.
  552.  
  553. LIMITATIONS:None Known.
  554.  
  555. SEE ALSO:   The global variable "lacer"
  556.  
  557. :*/
  558. void SetRGB8(reg,rr,gg,bb,base)
  559.   short reg;
  560.   unsigned char rr,gg,bb;
  561.   short base;
  562.   {
  563.   short p_row,p_index;
  564.     p_row = (reg >> 6) + base;           /* palette row 0-3.  */
  565.     p_index = ((reg & 0x3f) * 3) + 8;    /* reg 0-63 in p_row */
  566.     if (lacer) /* then we need both fields! */
  567.       {
  568.         /* for the even field: */
  569.         write_byte(p_index,   p_row*2, rr);         /* put RED value     */
  570.         write_byte(p_index+1, p_row*2, gg);         /* put GREEN value   */
  571.         write_byte(p_index+2, p_row*2, bb);         /* put BLUE value    */
  572.         
  573.         /* here is the stuff for the ODD field: */
  574.         write_byte(p_index,   (p_row*2)+1, rr);     /* put RED value     */
  575.         write_byte(p_index+1, (p_row*2)+1, gg);     /* put GREEN value   */
  576.         write_byte(p_index+2, (p_row*2)+1, bb);     /* put BLUE value    */
  577.       }
  578.     else /* just write to one field */
  579.       {
  580.         write_byte(p_index,   p_row, rr);     /* put RED value     */
  581.         write_byte(p_index+1, p_row, gg);     /* put GREEN value   */
  582.         write_byte(p_index+2, p_row, bb);     /* put BLUE value    */
  583.       }
  584.   }
  585.  
  586. void display_init (width, height)
  587.    int width, height;
  588.    {
  589.    Amiga_open();
  590.    open_requestor();
  591.  
  592.    Delay (10);
  593.  
  594.    if (DisplayFormat == 'E') {
  595.       if ((s = (struct Screen *) OpenScreen (&Ham_E_Screen)) == NULL)
  596.          exit (FALSE);
  597.       ShowTitle (s, FALSE);
  598.       lacer = 1;
  599.       fp0 = s->BitMap.Planes[0];
  600.       fp1 = s->BitMap.Planes[1];
  601.       fp2 = s->BitMap.Planes[2];
  602.       fp3 = s->BitMap.Planes[3];
  603.       make_hame_palette(&s->ViewPort);
  604.       SetAPen (&(s->RastPort), 0L);
  605.       RectFill (&(s -> RastPort), 0L, 0L, HAME_SCREEN_WIDTH-1, 1);
  606.       SetAPen (&(s->RastPort), 1L);
  607.       RectFill (&(s -> RastPort), 0L, 2L, HAME_SCREEN_WIDTH-1, HAME_SCREEN_HEIGHT-1);
  608.       write_cookie(ham_cookie, 0);
  609.       SetRGB8 (0x11, 0x80, 0x80, 0x80, 0);
  610.       }
  611.    else if (DisplayFormat == 'F') {
  612.     OpenFirecracker();
  613.       }
  614.    else {
  615.       if ((s = (struct Screen *) OpenScreen (&Ham_Screen)) == NULL)
  616.          exit (FALSE);
  617.  
  618.       ShowTitle (s, FALSE);
  619.  
  620.       LoadRGB4 (&(s->ViewPort), ColorTbl, 16L);
  621.       SetAPen (&(s->RastPort), 7L);
  622.       RectFill (&(s -> RastPort), 0L, 0L, 319L, 399L);
  623.       }
  624.    }
  625.  
  626. void display_close ()
  627.    {
  628.    if (Requestor_Running) {
  629.       Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  630.       Delay (2L);
  631.       }
  632.  
  633.    if (Requestor_Window)
  634.       FreeSysRequest (Requestor_Window);
  635.  
  636.    Requestor_Window = NULL;
  637.  
  638.    if (DisplayFormat != 'F')
  639.       CloseScreen (s);
  640.    }
  641.  
  642. #define absdif(x,y) ((x > y) ? (x - y) : (y - x))
  643. #define max3(x,y,z) ((x>y)?((x>z)?1:3):((y>z)?2:3))
  644.  
  645. void write_hame_pixel (x, y, Red, Green, Blue)
  646.    int x, y;
  647.    char Red, Green, Blue;
  648.    {
  649.    register unsigned char colour;
  650.    short delta_red, delta_green, delta_blue;
  651.  
  652.    if ((x >= SCREEN_WIDTH )  || (y >= SCREEN_HEIGHT))
  653.       return;
  654.  
  655.    Red = (Red >> 2) & 0x3F;
  656.    Green = (Green >> 2) & 0x3F;
  657.    Blue = (Blue >> 2) & 0x3F;
  658.  
  659.    if (last_y != y) {
  660.       last_y = y;
  661.       last_red = last_green = last_blue = 0;
  662.       }
  663.  
  664.    delta_red = absdif (Red, last_red);
  665.    delta_green = absdif (Green, last_green);
  666.    delta_blue = absdif (Blue, last_blue);
  667.  
  668.    switch (max3(delta_red, delta_green, delta_blue)) {
  669.       case 1:
  670.          last_red = Red;
  671.          colour = 0x80 + Red;
  672.          break;
  673.       case 2:
  674.          last_green = Green;
  675.          colour = 0xc0 + Green;
  676.          break;
  677.       case 3:
  678.          last_blue = Blue;
  679.          colour = 0x40 + Blue;
  680.          break;
  681.       }
  682.  
  683.    write_byte (x, y+2, colour);
  684.    }
  685.  
  686. void display_plot (x, y, Red, Green, Blue)
  687.    int x, y;
  688.    char Red, Green, Blue;
  689.    {
  690.    register short colour, index, mask, i, colour_mask;
  691.    register char *addr;
  692.    short delta_red, delta_green, delta_blue;
  693.  
  694.    if (DisplayFormat == 'E')
  695.       return (write_hame_pixel(x, y, Red, Green, Blue));
  696.    else if (DisplayFormat == 'F')
  697.     return (write_firecracker_pixel((UWORD) x, (UWORD) y, Red, Green, Blue));
  698.  
  699.    if ((x >= SCREEN_WIDTH )  || (y >= SCREEN_HEIGHT))
  700.       return;
  701.  
  702.    Red = (Red >> 4) & 0x0F;
  703.    Green = (Green >> 4) & 0x0F;
  704.    Blue = (Blue >> 4) & 0x0F;
  705.  
  706.    if (last_y != y) {
  707.       last_y = y;
  708.       last_red = last_green = last_blue = 0;
  709.       }
  710.  
  711.    delta_red = absdif (Red, last_red);
  712.    delta_green = absdif (Green, last_green);
  713.    delta_blue = absdif (Blue, last_blue);
  714.  
  715.    switch (max3(delta_red, delta_green, delta_blue)) {
  716.       case 1:
  717.          last_red = Red;
  718.          colour = 0x20 + Red;
  719.          break;
  720.       case 2:
  721.          last_green = Green;
  722.          colour = 0x30 + Green;
  723.          break;
  724.       case 3:
  725.          last_blue = Blue;
  726.          colour = 0x10 + Blue;
  727.          break;
  728.       }
  729.  
  730.    index = (SCREEN_WIDTH >> 3) * y + (x >> 3);
  731.    mask = 0x80 >> (x & 7);
  732.  
  733.    colour_mask = 1;
  734.  
  735.    for (i = 0 ; i < 6 ; i++) {
  736.       addr = &s->BitMap.Planes[i][index];
  737.       *addr &= ~mask;
  738.       *addr |= (colour&colour_mask) ? mask : 0x00;
  739.       colour_mask <<= 1;
  740.       }
  741.    }
  742.  
  743. int amiga_close_all ()
  744.    {
  745.    close_all();
  746.    return (1);
  747.    }
  748.